package org.xshaokuang.loginandemail.login;

import cn.nukkit.Player;
import cn.nukkit.form.element.ElementButton;
import cn.nukkit.form.element.ElementInput;
import cn.nukkit.form.element.ElementLabel;
import cn.nukkit.form.handler.FormResponseHandler;
import cn.nukkit.form.window.FormWindowCustom;
import cn.nukkit.form.window.FormWindowSimple;
import org.xshaokuang.loginandemail.MainPlugin;
import org.xshaokuang.loginandemail.email.EmailDataManager;

public class LoginForm {
    private final LoginDataManager manager;

    private EmailDataManager emailManager = MainPlugin.getInstance().getEmailDataManager();

    public LoginForm(LoginDataManager manager) {
        this.manager = manager;
    }

    public boolean isNameLegal(String name) {
        return !(name.length() < 4 || name.contains("."));
    }

    public void sendForm(Player player) {
        if(!isNameLegal(player.getName())) {
            player.kick("名字不合法", false);
            return;
        }
        if(!manager.has(player.getName())) {
            sendRegisterForm(player);
        } else if(manager.needsLogin(player.getName(), player.getAddress())) {
            sendLoginForm(player);
        } else {
            loginSuccess(player);
        }
    }

    public void sendLoginForm(Player player) {

        FormWindowCustom form = new FormWindowCustom("登录");
        form.addElement(new ElementInput("输入密码:", "", ""));
        form.addElement(new ElementLabel("忘记密码可联系腐竹"));

        form.addHandler(FormResponseHandler.withoutPlayer(ignored -> {
            if (form.wasClosed()) {
                player.kick("请完成登录", false);
                return;
            }
            if(!manager.validatePassword(player.getName(), form.getResponse().getInputResponse(0))) {
                player.kick("密码错误", false);
            } else {
                loginSuccess(player);
            }
        }));
        player.showFormWindow(form);
    }

    private void sendRegisterForm(Player player) {
            FormWindowCustom form = new FormWindowCustom("注册");
            form.addElement(new ElementInput("输入密码:", "", ""));
            form.addElement(new ElementInput("确认密码:", "", ""));
            form.addHandler(FormResponseHandler.withoutPlayer(ignored -> {
                if (form.wasClosed()) {
                    player.kick("请完成注册与登录", false);
                    return;
                }
                if((form.getResponse().getInputResponse(0)) != form.getResponse().getInputResponse(0)) {
                    sendRegisterFailWindow(player);

                }
                sendBindForm(player, form.getResponse().getInputResponse(0));
            }));
         player.showFormWindow(form);
    }

    public static void sendRegisterFailWindow(Player player) {
        FormWindowSimple window = new FormWindowSimple("注册失败", "两次输入密码不一致");
        window.addButton(new ElementButton("重新注册"));
        window.addHandler(FormResponseHandler.withoutPlayer(ignored -> {
            if (window.wasClosed()) {
                player.kick("请完成注册与登录", false);
                return;
            }
            if (window.getResponse().getClickedButtonId() == 0) {
                sendRegisterFailWindow(player);
            }
        }));
        player.showFormWindow(window);
    }

    private void sendBindForm(Player player, String password)  {
        FormWindowCustom form = new FormWindowCustom("绑定");
        form.addElement(new ElementLabel("输入QQ可进行绑定（用于找回密码）"));
        form.addElement(new ElementInput("QQ:", "", "不绑定"));
        form.addHandler(FormResponseHandler.withoutPlayer(ignored -> {
            if (form.wasClosed()) {
                player.kick("请完成注册与登录", false);
                return;
            }
            String QQ = "0";

                QQ = form.getResponse().getInputResponse(1).trim();
            manager.addOrUpdatePlayer(player.getName(), password, player.getAddress(), QQ);
            sendLoginForm(player);
        }));
        player.showFormWindow(form);
    }

    public void loginSuccess(Player player) {
        player.sendMessage("§a欢迎回来，" + player.getName() + "！");
        emailManager.loadFromConfig(player.getName());
        if(emailManager != null && emailManager.hasEmail(player)) {
            player.sendMessage("§a你有" + emailManager.unreadEmailNum(player) + "封未读邮件，输入/xemailopen查看。");
        }
        manager.upDateTimeAndIp(player);
        manager.remove(player.getName());
    }
}

